<?php defined('SYSPATH') or die('No direct script access.');
/**
* @package Fatima Purple Project <FPP>
* @author Erik Beltran Estelita <WindMaker> <hunk4ths@gmail.com>
* @description Crud Model & ORM for Kohana :)
* @license MIT
* @date lun nov  9 23:34:36 PET 2009
*/
class fpp_tdp_Model extends Model {

  function __construct($DataObjects){
    parent::__construct();
    $this->field_name   = $DataObjects->field_name;
    $this->field_value  = $DataObjects->field_value;
    $this->args         = $DataObjects->arguments;
    $this->attributes   = $DataObjects->attributes;
    $this->PkID         = $DataObjects->PkID;
    $this->execthis();
  }

  private function execthis(){
    $value = $this->field_value;
    $field = $this->field_name;
    if($this->args->exec <> null){
      eval($this->args->exec.';');
    }
    $this->field_value = $value;
  }
  
  /**
  * constructor de Link y Ajax javascript
  * requiere obligadamente algunos Atributos como el Nombre del Model actual
  * argumentos : db_table 
  */
  protected function AjaxConstruct($field_html){
    $table_this = substr($this->attributes->NameModel,'4','-6');
    $uri = new Uri;
    $uri_string = $uri->string();
    $uri_string = str_replace($table_this,$this->args->db_table,$uri_string);
    $uri_string_save = $uri_string;
    $uri_string = explode('/edit/',$uri_string);    
    $bl = count ($uri_string);
    if(count ($uri_string) == 1 ){
      $uri_string = explode('/add',$uri_string_save);
    }
    $uri_string_ajax = url::base(FALSE).$uri_string[0].'/ajax/'.$this->PkID.'/ajaxfield/'.$this->field_name;
    $uri_string_ajax = str_replace(array("mod/".$this->args->db_table,'tbl_'.$this->args->db_table),
                                   array("mod/".$table_this,'tbl_'.$table_this), $uri_string_ajax);
    $_SESSION['PKID'] = $this->PkID;
    $uri_string = $uri_string[0].'/add';
    $href = fpp::iconify('Agregar','plusthick');
    $props = array(
                    "class"=>'AjaxOTO',
                    "id"=>'AjaxTBL_'.$this->field_name,
                    "logme" => $bl,
                    'refresh_to'=>$uri_string_ajax,
                    'field_name'=>$this->field_name);    
    $href = html::anchor($uri_string,$href,$props);
    $div = "<div class='AjaxTBL_{$this->field_name} DIVJAX' style='display:none;' ></div>";
    return $div."<table border=0><tr><td>".$field_html.'</td><td>'.$href.'</td></tr></table>';
  }
  
}